package org.donald.javascript;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Java 8提供了一个新的Nashorn javascript引擎，它允许我们在JVM上运行特定的javascript应用。Nashorn
 * javascript引擎只是javax.script.ScriptEngine另一个实现，而且规则也一样，允许Java和JavaScript互相操作
 * 
 * @author donald 2018年3月14日 下午10:22:06
 */
public class JavascriptTest {
	private static final Logger log = LoggerFactory.getLogger(JavascriptTest.class);
	public static void main(String[] args) throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		log.info(engine.getClass().getName());
		log.info("Result:{}",engine.eval("function f() { return 1; }; f() + 1;"));
	}

}
